<template>
  <div>
<van-datetime-picker
  v-model="currentDate"
  type="date"
  title="选择年月日"
  :min-date="minDate"
  :max-date="maxDate"
  @confirm="onConfirm"
  @cancel="$emit('close')"
/>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import { Toast } from 'vant'
import { updateUserProfile } from '@/api/user.js'
export default {
  data () {
    return {
      minDate: new Date(1900, 0, 1),
      maxDate: new Date(),
      currentDate: null
    }
  },
  props: {
    value: {
      type: String,
      required: true
    }
  },
  created () {
    this.currentDate = new Date(this.value)// 更新上去最新选择时间
  },

  methods: {
    async onConfirm (value) { // value format 在dayjs插件中 为了取打印出来的正规值
      Toast.loading({
        message: '发布中...',
        duration: 0,
        forbidClick: true
      })
      const formatTime = dayjs(value).format('YYYY-MM-DD')
      try {
        await updateUserProfile({
          birthday: formatTime
        })
        Toast.success('保存成功')
        this.$emit('close')
        this.$emit('input', formatTime)
      } catch (error) {
        Toast.fail('保存失败')
      }
      console.log(formatTime)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
